
package controllers;

import models.IdeaDeveloper;
import models.Log;

import com.google.gson.JsonObject;

import play.mvc.Controller;
import play.mvc.With;

/**
 * @author Maryam
 */
@With(Secure.class)
public class ChangePassword extends Controller {

	public static void ChangePassword() {
		render();
	}

	/**
	 * This methods takes the old password of the user and checks if it is
	 * correct then changes the password to the new password specified by the
	 * user
	 * 
	 * @author Maryam
	 * @param oldPassword
	 *            The old password of the user
	 * @param newPassword
	 *            The password that the user wants to change to
	 * 
	 */

	public static void changePass(String oldPassword, String newPassword) {

		JsonObject json = new JsonObject();

		Long loggedInUserID = Security.userid();

		IdeaDeveloper ideadev = IdeaDeveloper.findById(loggedInUserID);

		if ((ideadev.password.compareTo(oldPassword)) == 0) {

			ideadev.changePassword(newPassword);

			json.addProperty("done", "Password is changed Successfully");
			
			String body = "Hey " + ideadev.fullName + ", "  + "you recently changed your password. As a security precaution, this notification has been sent to all email addresses associated with your account.";
			ideadev.sendEMail(ideadev.eMail, "Password Changed", body);
			renderJSON(json.toString());
		} else {
			json.addProperty("done", "yarab");
			json.addProperty("done", "Sorry old password is incorrect");
			renderJSON(json.toString());
		}

	}
}
